package com.colin.aiyi.ui.login;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.colin.aiyi.R;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.entity.User;
import com.colin.aiyi.entity.response.UserInfoResponse;
import com.colin.aiyi.ui.BaseActivity;
import com.colin.aiyi.ui.Home.MainActivity;
import com.colin.aiyi.ui.RegisterAc;
import com.colin.aiyi.ui.ResetPasswdAc;
import com.colin.aiyi.utils.SharePreferenceUtil;
import com.colin.aiyi.view.ShowMsgUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by hl on 2017/5/5.
 */

public class LoginAc extends BaseActivity implements View.OnClickListener {
    UserInfoResponse user = new UserInfoResponse();

    @BindView(R.id.img_logo)
    ImageView imgLogo;
    @BindView(R.id.reset_pw)
    TextView resetPw;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_vcode)
    EditText editVcode;
    @BindView(R.id.text_msg)
    TextView textMsg;
    @BindView(R.id.button_send)
    TextView buttonSend;
    int fromAc;
    @BindView(R.id.text_zhuce)
    TextView textZhuce;
    @BindView(R.id.text_banner_left)
    TextView textBannerLeft;
    @BindView(R.id.text_banner_title)
    TextView textBannerTitle;
    @BindView(R.id.text_banner_rigth)
    TextView textBannerRigth;
    @BindView(R.id.linear)
    LinearLayout linear;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_login);
        ButterKnife.bind(this);

        textBannerLeft.setOnClickListener(this);
        textBannerTitle.setText("");
        resetPw.setText(Html.fromHtml("<u>找回密码</u>"));
        textZhuce.setText(Html.fromHtml("无账户   <u>注册</u>"));
        if (getIntent() != null) {
            fromAc = getIntent().getIntExtra("FromAc", 0);
        }
        linear.setBackgroundColor(Color.alpha(100));
    }

    @Override
    protected void onResume() {
        super.onResume();
        iPresenter = new ImpPLogin();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getUerInfo(UserInfoResponse user) {
        ShowMsgUtil.hideLoadingDialog();
        if (user.getCode() == 200) {
            SharePreferenceUtil.saveObject(this, user.getData());
            JPushInterface.setAlias(this, user.getData().getTelNo(), new TagAliasCallback() {
                @Override
                public void gotResult(int i, String s, Set<String> set) {
                    System.out.println("设置别名返回结果："+i);
                }
            });
            goToMain();
        } else {
            SharePreferenceUtil.saveObject(this, User.getInstance());
        }
    }


    private void goToMain() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    public void onBackPressed() {
        if (fromAc == Constant.MIAN) {
            goToMain();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_banner_left:
                onBackPressed();
                break;
            case R.id.reset_pw:
                startActivity(new Intent(this, ResetPasswdAc.class));
                break;
            case R.id.button_send:
                if (checkEmpty(new String[]{"输入正确手机号", "输入密码"}, new TextView[]{editPhone, editVcode}, textMsg)) {
                    User user = User.getInstance();
                    user.setTelNo(editPhone.getText().toString().trim());
                    user.setPassword(editVcode.getText().toString().trim());
                    iPresenter.login_login(user);
                    ShowMsgUtil.showLoadingDialog(getContext());
                }
                hideSoftInputFromWindow(editPhone);
                break;
            case R.id.text_zhuce:
                startActivity(new Intent(this, RegisterAc.class));
                break;
        }
    }


}
